const { getUserInfo } = require('../../utils/user')
const common = require('../../utils/common')
const { queryWithdrewApplyInfo, confirmWithdrewApply, sendWithdrewSmsCode, queryWithdrewConfigure } = require('../../services/withdrew')
const { MINPICKUP } = require('../../utils/constant')
Page({
  data: {
    MINPICKUP,
    shopVo: {},
    widthDrawMoney: null,
    bankCardNo: '',
    endBankCardNo: '',
    enableSubmit: false,
    dialogType: 0,
    widthDrawEnable: false,
    codeLength: 4,
    smsCode: '',
    isFocus: false,
    isGetCodeShow: true,
    lastTime: 60,
    widthdrawInfo: {},
    sliceMobile: '',
    withdrewConfigure: {
      bankCardNo: "6222021234567890123",
      bankIcon: "",
      bankName: "",
      totalAmount: 0.00,
    }
  },
  onLoad: function () {
    this.initData();
    this.getConfig()
  },
  getConfig: function () {
    queryWithdrewConfigure().then((res) => {
      this.updateConfig(res.data, () => {
        this.sliceBankCardNo()
      })
    }).catch(err => {
      common.log(err)
    })
  },
  updateConfig: function (payload, callback) {
    this.setData({
      withdrewConfigure: { ...this.data.withdrewConfigure, ...payload }
    }, () => {
      callback && callback()
    })
  },
  initData: function () {
    this.setData({
      shopVo: getUserInfo().shopVo || {},
    });
  },
  sliceBankCardNo: function () {
    const bankCardNo = this.data.withdrewConfigure.bankCardNo
    const endBankCardNo = bankCardNo && bankCardNo.toString().slice(-4)
    this.setData({
      endBankCardNo
    })
  },
  moneyChange: function (e) {
    const money = e.detail.value
    this.setData({
      widthDrawMoney: money
    })
    this.validateCanSubmit(money)
  },
  formatMoney: function () {
    if (!this.data.widthDrawMoney) return
    const money = parseFloat(this.data.widthDrawMoney).toFixed(2)
    this.setData({
      widthDrawMoney: money
    })
  },
  validateCanSubmit(money) {
    money = parseFloat(money)
    let enable = false
    if (money >= MINPICKUP && money <= parseFloat(this.data.withdrewConfigure.totalAmount)) {
      enable = true
    } else {
      enable = false
    }
    this.setData({
      enableSubmit: enable
    })
  },
  getAllMoney() {
    const money = parseFloat(this.data.withdrewConfigure.totalAmount)
    this.setData({
      widthDrawMoney: money,
    }, this.formatMoney)
    this.validateCanSubmit(money)
  },
  submitForm: function () {
    if (!this.data.enableSubmit) return
    queryWithdrewApplyInfo({ amount: parseFloat(this.data.widthDrawMoney) }).then((res) => {
      this.setData({
        widthdrawInfo: res.data,
        dialogType: 1
      })
    }).catch((err) => {
      common.log(err)
    })
  },
  closeDialog() {
    this.setData({
      dialogType: 0
    })
  },
  //请求获取手机验证码
  sendVerificationCode() {
    sendWithdrewSmsCode().then(res => {
      const sliceMobile = res.data.mobile.toString().slice(-4)
      this.setData({
        dialogType: 2,
        sliceMobile
      })
      //倒计时
      this.countDwonVerify();
    }).catch(err => {
      common.log(err);
    });
  },
  // 确认提现验证码校验
  confirmVerificationCode() {
    const { actualAmount, amount, taxAmount, brokerageAmount, surplusAmount } = this.data.widthdrawInfo
    const smsCode = this.data.smsCode
    const data = { actualAmount, amount, taxAmount, smsCode, brokerageAmount }
    confirmWithdrewApply(data).then(res => {
      if (res.data === true) {
        this.closeDialog()
        common.showToast(res.message)
        this.updateConfig({
          totalAmount: surplusAmount
        }, () => {
          this.moneyChange({ detail: { value: null } })
        })
      } else if (res.data && res.data.actualAmount <= 0) {
        common.showModal('依据国家法律规定，您当前的提现扣除税费后，实际提现金额小于0。暂时无法提现，待下月可继续进行提现。')
      } else {
        common.showToast(res.message)
      }
    }).catch(err => {
      common.log(err);
    });
  },
  // 隐藏的输入框聚焦唤起键盘
  onCodeFocus: function () {
    this.setData({ isFocus: true });
  },
  setCodeValue: function (e) {
    const code = e.detail.value
    if ((code + '').length === this.data.codeLength) {
      this.setData({
        widthDrawEnable: true
      })
    } else {
      this.setData({
        widthDrawEnable: false
      })
    }
    this.setData({ smsCode: code });
  },
  //更新界面60秒后获取验证码
  countDwonVerify: function () {
    // 将获取验证码按钮隐藏60s，60s后再次显示
    this.setData({
      isGetCodeShow: false
    })
    let countdown = 60;
    const settime = () => {
      if (countdown === 0) {
        this.setData({
          isGetCodeShow: true
        })
        countdown = 60;
        return;
      }
      this.setData({
        isGetCodeShow: false,
        lastTime: countdown
      })
      countdown--;
      setTimeout(function () {
        settime()
      }, 1000)
    }
    settime();
  },
})
